import 'dart:io';
import 'package:flutter_blue_plus/flutter_blue_plus.dart';
import 'package:get/get_utils/src/platform/platform.dart';
import 'package:location/location.dart';
import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:smart_translator/app/utils/tips.dart';

import '../logs/Logger.dart';


class PermissionUtil {
  static final _location = Location();
  static Future<bool> locationDisabled() async {
    if (Platform.isAndroid) {
      var serviceEnabled = await _location.serviceEnabled();
      if (!serviceEnabled) {
        return true;
      }
      return false;
    }
    return false;
  }

  // check bluetooth permission
  static Future<bool> bluetoothDisabled() async {
    return await FlutterBluePlus.adapterState.first ==
        BluetoothAdapterState.off;
  }

  // listen network state
  static void listenNetworkState() async {
    Connectivity().onConnectivityChanged.listen((event) {
      logger.info("network state change: $event");
      if (event == ConnectivityResult.none) {
        Tips.showToast('网络有问题.');
      }
    });
  }


  static void requestPermission() async {
    var statuses = {};
    if (GetPlatform.isAndroid) {
        statuses = await [
        Permission.locationWhenInUse,
        Permission.bluetoothConnect,
        Permission.bluetoothAdvertise,
        Permission.bluetoothScan
      ].request();
    } else {
      statuses = await [
        Permission.bluetoothConnect,
        Permission.bluetoothAdvertise,
        Permission.bluetoothScan
      ].request();
    }



    logger.info('request permission: ${statuses}');


  }


  static Future<bool> checkPermission() async {
    if (GetPlatform.isAndroid) {
      var status1 = await Permission.locationWhenInUse.status;
      var status2 = await Permission.bluetoothConnect.status;
      var status3 = await Permission.bluetoothScan.status;

      if (status1.isGranted && status2.isGranted && status3.isGranted) {
        return true;
      } else {
        return false;
      }
    } else {
      // var status2 = await Permission.bluetoothConnect.status;
      // var status3 = await Permission.bluetoothScan.status;
      //
      // if (status2.isGranted && status3.isGranted) {
      //   return true;
      // } else {
      //   return false;
      // }
      return true;
    }

  }
}